package com.example.ruralservicedigitalplatform.controller.admin;

import com.example.ruralservicedigitalplatform.comment.Result;
import com.example.ruralservicedigitalplatform.domain.po.Admin;
import com.example.ruralservicedigitalplatform.domain.query.PageBean;
import com.example.ruralservicedigitalplatform.service.AdminService;
import com.example.ruralservicedigitalplatform.utils.JwtUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Tag(name = "管理员列表")
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @PostMapping("/login")
    @Operation(summary = "管理员登录")
    public Result login(@RequestBody Admin admin) {
        Admin admin1 = adminService.login(admin);
        //登录成功，生成令牌，下发令牌
        if (admin1 != null) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("adminId", admin1.getAdminId());
            claims.put("adminName", admin1.getAdminName());
            String jwt = JwtUtils.generateJwt(claims);//jwt包含了当前登录的员工信息
            return Result.success(jwt);
        }
        //登录失败，返回错误信息
        return Result.error("管理员名错误或密码错误");
    }
    //分页查询
    @Operation(summary = "管理员分页查询")
    @GetMapping("/all")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10")Integer pageSize,
                       String adminName,Integer gender,String phoneNumber,Integer age,String image,String idNumber,
                       @DateTimeFormat(pattern="yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern="yyyy-MM-dd")LocalDate end){
        log.info("分页查询,参数:{},{},{},{},{},{},{},{},{}",page,pageSize,adminName,gender,phoneNumber,age,image,idNumber,begin,end);
        //调用service方法进行分页查询
        PageBean pageBean= adminService.page(page,pageSize,adminName,gender,phoneNumber,age,image,idNumber,begin,end);
        return Result.success(pageBean);
    }
    //更改管理员信息
    @Operation(summary = "更改管理员信息")
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        log.info("更改管理员信息：{}",admin);
        adminService.update(admin);
        return Result.success();
    }
    //新增管理员
    @PostMapping
    @Operation(summary = "新增管理员")
    public Result add(@RequestBody Admin admin){
        log.info("新增管理员:{}",admin);
        adminService.add(admin);
        return Result.success();
    }
    //删除管理员
    @DeleteMapping("/{ids}")
    @Operation(summary = "批量删除管理员")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("批量删除管理员,ids:{}",ids);
        adminService.delete(ids);
        return Result.success();
    }
}
